package com.zengde.ai_sandbox.controller;

import com.zengde.ai_sandbox.manager.BigModelManager;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
public class BigModelController {
    
    private final BigModelManager bigModelManager;
    
    /**
     * 创建会话
     */
    @PostMapping("/session")
    public Map<String, String> createSession(
            @RequestParam String customerId, 
            @RequestParam(defaultValue = "doubaoService") String modelServiceName) {
        
        String sessionId = bigModelManager.createSession(customerId, modelServiceName);
        
        // 获取机器人的首次发言
        String firstMessage = bigModelManager.robotFirstMessage(sessionId);
        
        Map<String, String> response = new HashMap<>();
        response.put("sessionId", sessionId);
        response.put("firstMessage", firstMessage);
        
        return response;
    }
    
    /**
     * 非深度思考、非流式调用
     */
    @PostMapping("/ntns")
    public Map<String, String> ntns(
            @RequestParam String sessionId, 
            @RequestParam String content) {
        
        String response = bigModelManager.ntns(sessionId, content);
        
        Map<String, String> result = new HashMap<>();
        result.put("response", response);
        
        return result;
    }
    
    /**
     * 非深度思考、流式调用
     */
    @PostMapping(value = "/nts", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> nts(
            @RequestParam String sessionId, 
            @RequestParam String content) {
        
        return bigModelManager.nts(sessionId, content);
    }
    
    /**
     * 深度思考、流式调用
     */
    @PostMapping(value = "/ts", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> ts(
            @RequestParam String sessionId, 
            @RequestParam String content) {
        
        return bigModelManager.ts(sessionId, content);
    }
    
    /**
     * 深度思考、非流式调用
     */
    @PostMapping("/tns")
    public Map<String, String> tns(
            @RequestParam String sessionId, 
            @RequestParam String content) {
        
        String response = bigModelManager.tns(sessionId, content);
        
        Map<String, String> result = new HashMap<>();
        result.put("response", response);
        
        return result;
    }
    
    /**
     * 图像聊天
     */
    @PostMapping("/imgChat")
    public Map<String, String> imgChat(
            @RequestParam String sessionId, 
            @RequestParam String content,
            @RequestParam String imageUrl) {
        
        String response = bigModelManager.imgChat(sessionId, content, imageUrl);
        
        Map<String, String> result = new HashMap<>();
        result.put("response", response);
        
        return result;
    }
} 